package servlets;

import java.io.IOException;
import java.util.*;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import manager.*;
import NegocioVO.*;



public class ControladorMesa extends HttpServlet {

	private static final long serialVersionUID = 1087702007634924546L;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
        {
            String action = request.getParameter("action");
            String jspPage = null;
           
           
            if ((action == null) || (action.length() < 1))
            {
                action = "default";
            }

            if ("default".equals(action))
            {
            	String cookieName = "userLocal";
            	Cookie cookies[] = request.getCookies();
            	Cookie myCookie = null;
            	String aux = null;
            	if (cookies != null) {
            		for (int i = 0; i < cookies.length; i++) {
            			if (cookies[i].getName().equals(cookieName)) {
            				myCookie = cookies[i];
            				break;
            			}
            		}
            	}
            	String LocalId;
            	LocalId = myCookie.getValue();
            	List<MesaVO> mesas = new ArrayList<MesaVO>();
            	mesas = AdministradorMesa.getInstancia().getMesas(LocalId);
				request.setAttribute("mesas", mesas);
                jspPage = "/jsp/AbrirMesa2.jsp";
            }
            
             else if ("abrirmesa".equals(action)){
           
            	String[] categorias = request.getParameterValues("checkbox");
            	 
            	 AdministradorMesa.getInstancia().abrirMesas(categorias);
/*            	 if(categorias!=null && categorias.length>0) {
            	      for(int i=0; i<categorias.length; i++) {
            	         System.out.println(categorias[i]);
            	      }
            	 	  }*/
            	 request.setAttribute("mensaje", "Todo Bien");
            	 jspPage = "/jsp/ResultadoAbrirMesa.jsp";
            	 
            	
            }
             else if ("cerrarMesa".equals(action)){
            	String cookieName = "userLocal";
             	Cookie cookies[] = request.getCookies();
             	Cookie myCookie = null;
             	String aux = null;
             	if (cookies != null) {
             		for (int i = 0; i < cookies.length; i++) {
             			if (cookies[i].getName().equals(cookieName)) {
             				myCookie = cookies[i];
             				break;
             			}
             		}
             	}
             	String LocalId;
             	LocalId = myCookie.getValue();
             	
             	List<MesaVO> mesasOc = new ArrayList<MesaVO>();
            	mesasOc = AdministradorMesa.getInstancia().getMesasOcupadas(LocalId);
				request.setAttribute("mesasOc", mesasOc);
                jspPage = "/jsp/CerrarMesa.jsp";
             	
             }
             else if ("cerrarmesaBOTON".equals(action)){
             	String[] cerrados = request.getParameterValues("checkbox2");
           	 AdministradorMesa.getInstancia().cerrarMesas(cerrados);
           	 request.setAttribute("mensaje", "Todo Bien");
           	 jspPage = "/jsp/ResultadoCerrarMesa.jsp";
              }
            System.out.println(action);
            dispatch(jspPage, request, response);
            //if (jspPage!= null)
            //{
            	//dispatch(jspPage, request, response);
           // }
        }
    
        protected void dispatch(String jsp, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
        {
            if (jsp != null)
            {
                RequestDispatcher rd = request.getRequestDispatcher(jsp);
                rd.forward(request, response);
            }
        }

        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
        {
            doPost(request, response);
        }
}
